package com.hengtongxing.hejiayun_employee.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hengtongxing.hejiayun_employee.R;
import com.hengtongxing.hejiayun_employee.base.BaseActivity;
import com.hengtongxing.hejiayun_employee.base.DataCallBack;
import com.hengtongxing.hejiayun_employee.bean.BaseResponseBean;
import com.hengtongxing.hejiayun_employee.bean.UserInfoBean;
import com.hengtongxing.hejiayun_employee.login.ForgetPwdActivity;
import com.hengtongxing.hejiayun_employee.login.LoginActivity;
import com.hengtongxing.hejiayun_employee.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun_employee.utils.SPManager;
import com.hengtongxing.hejiayun_employee.widget.AppTitleBar;
import com.hengtongxing.hejiayun_employee.widget.CommonNoTitleDialog;
import com.hengtongxing.hejiayun_employee.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_updata_mobile)
    LinearLayout llUpdataMobile;
    @BindView(R.id.ll_updata_pwd)
    LinearLayout llUpdataPwd;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_updata_name)
    LinearLayout llUpdataName;
    @BindView(R.id.ll_logout)
    LinearLayout llLogout;
    private CommonNoTitleDialog.Builder dialogBuild;
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            tvName.setText(userInfoBean.getData().getName());
        }
    }

    @OnClick({R.id.ll_updata_mobile, R.id.ll_updata_pwd, R.id.ll_logout, R.id.ll_updata_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_updata_mobile:
                showActivity(UpdateMobileActivity.class);
                break;
            case R.id.ll_updata_pwd:
                showActivity(ForgetPwdActivity.class);
                break;
            case R.id.ll_logout:
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("账户注销后将无法使用此账号登录");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    outLoginAccount();
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
                break;
            case R.id.ll_updata_name:
                if (userInfoBean == null) {
                    return;
                }
                Intent intent = new Intent(mActivity, UpdateNameActivity.class);
                intent.putExtra("name", userInfoBean.getData().getName());
                startActivity(intent);
                break;
        }
    }

    /**
     * 注销
     */
    public void outLoginAccount() {
        ViewLoading.showProgress(mActivity);
        new MineModelImpl().requestLoginOutAccount(new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                SPManager.getInstance().logOffRemove();
                Intent intent = new Intent(mActivity, LoginActivity.class);
                startActivity(intent);
                mActivity.finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
